// EverbirdArmrServerDlg.h : header file
//

#pragma once
#include "WinSocket2Communicator.h"
#include "PUDData.h"
#include <string>
#include "afxwin.h"

// CEverbirdArmrServerDlg dialog
class CEverbirdArmrServerDlg : public CDialog
{
// Construction
public:
	CEverbirdArmrServerDlg(CWnd* pParent = NULL);	// standard constructor

// Dialog Data
	enum { IDD = IDD_EVERBIRDARMRSERVER_DIALOG };

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);	// DDX/DDV support


// Implementation
public:
	DWORD SLEEP_REGULAR_SENDDATA;
	DWORD SLEEP_CONNECT_CLIENT_RETRY;
	DWORD SLEEP_WARNING_RETRY;
	DWORD SLEEP_OVERLOAD_RETRY;
	DWORD SLEEP_HEARTBEAT;
	DWORD SLEEP_HEARTBEAT_RETRY;
	int clientListenerPort;
	int clientSpeakerPort;
	int centerListenerPort;
	int centerSpeakerPort;
	bool isListenerStarted;
	bool isReceivedHeartbeatACK;
	bool isHeartbeatStarted;
	bool isClientInChecking[256];
	bool isClientOverload[256];
	CString IPbyId[256];
	int currentClientID;
	WinSocket2Communicator* m_comm;
	PUDData _pudData;
	char szBinaryFrame[BINARY_FRAME_SIZE];
	char szFrameString[MAX_DATA_LENGTH];
	ParseUtil _parseUtil;
protected:
	HICON m_hIcon;
	HANDLE m_commListenThread;
	HANDLE m_commSpeakThread;

	// Generated message map functions
	virtual BOOL OnInitDialog();
	afx_msg void OnPaint();
	afx_msg HCURSOR OnQueryDragIcon();
	DECLARE_MESSAGE_MAP()
public:
	afx_msg void OnBnClickedStochasticGetdataBtn();
public:
	void createSpeakThread(DWORD threadId);
	void createListenThread();
	void dealWithReceivedFrame();
	void dealWithFunctionCode();
	void receiveDataFromClient();
	void connectClientWarning(int retryTime);
	void sendWarningACK();
	void sendOverloadWarning();
	void sendHeartbeatChecking();
	void overloadWarning(int retryTime);

	void setBinaryFrame(char* szBinaryFrame);
	void setFrameString(char* szFrameString);

	void clearErrorWarning();
	void desSrcNotMatch();
	void notReceiveHeartbeatACK(int retryTime);

	void initIPbyId();
	void loadAllIPAndPortFromConfig();
	void loadSleepTimeFromConfig();
	void setupSelectedClientById(int Id);
public:
	afx_msg void OnBnClickedAlertBtn();
public:
	afx_msg void OnBnClickedStartCenterListenerBtn();
public:
	CEdit m_ctrlAV;
public:
	CEdit m_ctrlBV;
public:
	CEdit m_ctrlCV;
public:
	CEdit m_ctrlAA;
public:
	CEdit m_ctrlBA;
public:
	CEdit m_ctrlCA;
public:
	CEdit m_ctrlAW;
public:
	CEdit m_ctrlBW;
public:
	CEdit m_ctrlCW;
public:
	CEdit m_ctrlSendFrame;
public:
	CEdit m_ctrlReceiveFrame;
public:
	CEdit m_ctrlClientAddress;
public:
	CEdit m_ctrlLastSendTime;
public:
	CEdit m_ctrlLastReceiveTime;
public:
	CEdit m_ctrlSelfAddress;
public:
	CEdit m_ctrlErrStatus;
public:
	CEdit m_ctrlWarningMsg;
public:
	CComboBox m_ctrlClientId;
public:
	afx_msg void OnCbnSelchangeClientId();
public:
	CButton m_ctrlOverloadBtn;
public:
	afx_msg void OnBnClickedOverloadBtn();
public:
	afx_msg void OnBnClickedHeartbeatBtn();
public:
	CButton m_ctrlHeartbeatBtn;
public:
	CEdit m_ctrlDesAddress;
public:
	CEdit m_ctrlDestAddress;
public:
	CButton m_ctrlStartListener;
};
DWORD ListenerThread(CEverbirdArmrServerDlg* lpparam);
DWORD SpeakerThread(CEverbirdArmrServerDlg* lpparam);
DWORD SpeakerThreadOverload(CEverbirdArmrServerDlg* lpparam);
DWORD SpeakerThreadHeartbeat(CEverbirdArmrServerDlg* lpparam);
DWORD SpeakerThreadRequestData(CEverbirdArmrServerDlg* lpparam);

